'use strict';
exports.main = async (event, context) => {
	const db = uniCloud.database();
	const dbCmd = db.command;
	const action = event.action;
	let dbRes;
	if(action == "create"){
		// 数据库结构设计
		dbRes = await db.collection("order").add({
			username:event.username,
			create_time:String(Date()),
			order_price:event.order_price,//订单总金额
			goods:event.goods,//商品
			order_status:0,
			consignee_addr:event.consignee_addr //订单地址
		})
	}else if(action == "get"){
		dbRes = await db.collection("order").where({
			username:dbCmd.eq(event.username)
		}).orderBy("_id","desc").get()
	}else if(action=="update"){
		dbRes = await db.collection("order").where({
			_id:dbCmd.eq(event._id)
		}).update({
			order_status:1//改变订单状态为已经收货
		})
	}else if(action=="update"){
		dbRes = await db.collection("order").where({
			_id:dbCmd.eq(event._id)
		}).update({
			order_status:1
		})
	}else if(action == "delete"){
		dbRes = await db.collection("order").where({
			_id:dbCmd.eq(event._id)
		}).remove()
	}
	//返回数据给客户端
	return dbRes?dbRes.data:null;
};
